package com.mm.update.controller;

import com.mm.update.service.VosDeployService;
import com.mm.update.vo.DeployLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName: VosDeploy
 * Description:
 * date: 2023/5/22 16:29
 *
 * @author  
 * @since JDK 1.8
 */
@Slf4j
@RestController
@RequestMapping("/vosDeploy")
public class VosDeployController {
    @Resource
    VosDeployService vosDeployService;

    @Resource
    HttpServletRequest request;

   /* @RequestMapping(value = "/uploadJar", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    String uploadJar(@RequestParam String ip, @RequestParam String username, @RequestParam String password, @RequestParam MultipartFile file) {
        try {
            vosDeployService.uploadJar(ip, JasyptUtils.getMessage(username), JasyptUtils.getMessage(password), file);
            return "上传成功";
        } catch (Exception e) {
            log.error("发送失败,",e);
            return "上传失败" + e.getMessage();
        }
    }

    @RequestMapping(value = "/deploy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    String uploadJar(@RequestParam String ip, @RequestParam String username, @RequestParam String password, @RequestParam String jarName) {
        try {
            String result = vosDeployService.deploy(ip, JasyptUtils.getMessage(username), JasyptUtils.getMessage(password), jarName);
            return "发布成功"+result;
        } catch (Exception e) {
            log.error("发布失败,e");
            return "发布失败" + e.getMessage();
        }
    }*/

    @RequestMapping(value = "/uploadAndDeploy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    String uploadAndDeploy(@RequestParam String ip, @RequestParam String username, @RequestParam String password, @RequestParam MultipartFile file) {
        try {
//            log.error("发布记录,userip{},ip{},username{},password{},filename{}", IpUtil.getIpAddr(request),ip,username,password,file.getOriginalFilename());
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            LogUtil.printLog(ip+"|"+username+"|"+file.getOriginalFilename()+"|"+IpUtil.getIpAddr(request)+"|"+ sdf.format(new Date()));
//            vosDeployService.uploadAndDeployJar(ip, username, password, file,new DeployLog(ip,IpUtil.getIpAddr(request), sdf.format(new Date()),file.getOriginalFilename(),username));
            return "浏览器打开http://127.0.0.1/vosupdate/index发布";
        } catch (Exception e) {
            log.error("发送失败,",e);
            return "上传失败" + e.getMessage();
        }
    }

    @RequestMapping(value = "/getDeployList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    List<DeployLog> getDeployList() {
        try {
            return vosDeployService.getDeployList();
        } catch (Exception e) {
            log.error("查询失败,",e);
            return null;
        }
    }



   /* @RequestMapping(value = "/uploadBatch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    String uploadBatch(@RequestParam String ip, @RequestParam String username, @RequestParam String password, @RequestParam List<MultipartFile> file) {
        try {
            vosDeployService.uploadBatch(ip, JasyptUtils.getMessage(username), JasyptUtils.getMessage(password), file);
            return "上传成功";
        } catch (Exception e) {
            log.error("发送失败,",e);
            return "上传失败" + e.getMessage();
        }
    }


    @RequestMapping(value = "/deployBatch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    String deployBatch(@RequestParam String ip, @RequestParam String username, @RequestParam String password) {
        try {
            String result = vosDeployService.deployBatch(ip, JasyptUtils.getMessage(username), JasyptUtils.getMessage(password));
            return "发布成功"+result;
        } catch (Exception e) {
            log.error("发布失败,e");
            return "发布失败" + e.getMessage();
        }
    }
*/


}
